class AnalysisTools {
    // 组合geoserver的url
    zhGeoserverUrl(obj: any, existingUrl: any, uppercase?: any) {
        var params = []
        for (var i in obj) {
            params.push(encodeURIComponent(uppercase ? i.toUpperCase() : i) + '=' + encodeURIComponent(obj[i]))
        }
        return ((!existingUrl || existingUrl.indexOf('?') === -1) ? '?' : '&') + params.join('&')
    }
    //获取wfs的服务的属性信息
    /**
     * 
     * @param basicUrl http://api.tianditu.gov.cn/,wfs服务地址
     * @param workspace LangXi,图层名称
     * @param code aviation_ranger,图层编码
     * @param filter 筛选规则
     * @returns 
     */
    async getwfsMsg(basicUrl: any, workspace: any, code: any, filter: any) {
        let options = {
            url: basicUrl + workspace,
            layers: workspace + ":" + code
        };
        let param = {
            service: "WFS",
            version: "1.0.0",
            request: "Getfeature",
            typeName: options["layers"],
            outputFormat: "application/json"
        };
        let url = options["url"] + "/ows" + this.zhGeoserverUrl(param, options["url"]);
        if (filter) {
            url = url + "&cql_filter=" + filter
        }
        let response = await fetch(url,{
            method: 'GET',
            headers: {
                'Content-Type': 'application/json'
            },
            mode: 'cors'
        })
        let result=await response.json();
        return result
    }
    /**
     * 
     * @param startPoint [136,72],开始位置
     * @param endPoint [136,72],结束位置
     * @param key 高德key
     * @returns 
     */
    //获取高德的路径规划
    async getGaoDePlanPath(startPoint: Array<string>, endPoint: Array<string>, key: any) {
        let url = `https://restapi.amap.com/v3/direction/driving?origin=${startPoint.join(',')}&destination=${endPoint.join(',')}&extensions=all&strategy=2&output=json&key=${key}`;
        let response= await fetch(url,{
            method: 'GET',
            headers: {
                'Content-Type': 'application/json'
            },
            mode: 'cors'
        });
        let result=await response.json();
        return result;
    }
    /**
     * 
     * @param startPoint "136,72",开始位置
     * @param endPoint "136,72",结束位置
     * @param key 天地图key
     * @returns 
     */
    //获取天地图的路径规划
    async getTdtPlanPath(startPoint: string, endPoint: string, key: any) {
        let url = `http://api.tianditu.gov.cn/drive?postStr={"orig":${startPoint},"dest":${endPoint},"style":"0"}&type=search&tk=${key}`;
        let response= await fetch(url,{
            method: 'GET',
            headers: {
                'Content-Type': 'application/json'
            },
            mode: 'cors'
        });
        let result=await response.json();
        return result;
    }
    /**
     * 
     * @param keyword "北京市",搜索关键字
     * @param xzqbm "10010",行政区代码
     * @param key 高德地图key
     * @returns 
     */
    //获取高德的地名地址查询
    async getGaoDeLocation(keyword: any, xzqbm: any, key: any) {
        let url = `https://restapi.amap.com/v5/place/text?key=${key}&keywords=${keyword}&region=${xzqbm}`
        let response= await fetch(url,{
            method: 'GET',
            headers: {
                'Content-Type': 'application/json'
            },
            mode: 'cors'
        });
        let result=await response.json();
        return result;
    }
    /**
     * 
     * @param keyword "北京市",搜索关键字
     * @param xzqbm "10010",行政区代码
     * @param tdtKeys 天地图key
     * @param mapBound [123,123,123,123],搜索范围
     * @returns 
     */
    //获取天地图的地名地址查询
    async getTdtLocation(keyword: any, xzqbm: any, tdtKeys: any, mapBound: any) {
        let postStr = {
            'keyWord': keyword,
            'level': '15',
            'mapBound': mapBound,
            'queryType': '7',
            'count': '100',
            'start': '0',
            'queryTerminal': '10000',
            'specifyAdminCode': xzqbm
        }
        let url = 'http://api.tianditu.gov.cn/search?postStr=' + JSON.stringify(postStr) + '&type=query&tk=' + tdtKeys;
        let response= await fetch(url,{
            method: 'GET',
            headers: {
                'Content-Type': 'application/json'
            },
            mode: 'cors'
        })
        let result=await response.json();
        return result;
    }
}
export default new AnalysisTools()
